# -*- coding:utf-8 -*-


"""
@version: v1.0
@author: 夏伟坤
@Email: 915624876@qq.com
@software: PyCharm
@project: ParsingExcelTables
@file: log_helper.py
@time: 2021/10/22 21:11
"""


import os
import datetime
import logging
from logging import handlers


class LogT:
    @staticmethod
    def beijing(seq, what):
        beijing_time = datetime.datetime.now() + datetime.timedelta(hours=8)
        return beijing_time.timetuple()

    @staticmethod
    def _logging(**kwargs):
        level = kwargs.pop('level', None)
        filename = kwargs.pop('filename', None)
        datefmt = kwargs.pop('datefmt', None)
        format = kwargs.pop('format', None)
        if level is None:
            level = logging.DEBUG
        if filename is None:
            filename = 'default.log_helper'
        if datefmt is None:
            datefmt = '%Y-%m-%d %H:%M:%S'
        if format is None:
            format = '%(asctime)s [%(module)s] %(levelname)s [%(lineno)d] %(message)s'

        log = logging.getLogger(filename)
        # 控制时间，在容器内 需要+8小时
        # logging.Formatter.converter = LogT.beijing
        format_str = logging.Formatter(format, datefmt)
        # backupCount 保存日志的数量，过期自动删除
        # when 按什么日期格式切分(这里方便测试使用的秒)
        # 文件输出
        th = handlers.TimedRotatingFileHandler(filename=filename, when='D', backupCount=7, encoding='utf-8')
        th.setFormatter(format_str)
        th.setLevel(logging.INFO)

        # 控制台输出
        stream_handler = logging.StreamHandler()  # 日志控制台输出
        stream_handler.setFormatter(format_str)
        stream_handler.setLevel(logging.INFO)

        log.addHandler(th)
        log.addHandler(stream_handler)
        log.setLevel(level)
        return log


os.makedirs("../logs", exist_ok=True)
logger = LogT._logging(filename='../logs/default.log')

if __name__ == '__main__':

    logger.info("文件启动")
    logger.error("文件启动")
    logger.exception("文件启动")
    logger.warning("文件启动")